# -*- coding:UTF-8 -*-
from selenium import webdriver
from openpyxl import Workbook

allData = []
# webdriver 驱动路径
path = 'E:/desktop/chromedriver_win32/chromedriver.exe'
driver = webdriver.Chrome(executable_path=path)
url = 'https://www.6baike.com/wiki/HuNanLiShi/index.html'
driver.get(url)
history_list = driver.find_elements_by_css_selector('#content > div.cwz.list li a')
length = history_list.__len__()
for i in range(length):
    history_list = driver.find_elements_by_css_selector('#content > div.cwz.list li a')
    a = history_list[i]
    text = a.text
    if '历史名人' in text:
        print(text)
        try:
            a.click()
        except Exception as e:
            print(e)
            continue
        figureNames = {}
        try:
            figureNames = driver.find_elements_by_css_selector('#content > div.arc > li > ul li')
            if figureNames.__len__() == 0:
                figureNames = driver.find_elements_by_css_selector('#content > div.arc > li b')
        except Exception as e:
            print(e)
            try:
                figureNames = driver.find_elements_by_css_selector('#content > div.arc > li > b')
                figures = []
                for item in figureNames:
                    figures.append(item.text.strip('，'))
                allData.append({text: figures})
            except Exception as ex:
                print(ex)
                continue
        figures = []
        for item in figureNames:
            figures.append(item.text.strip('，'))
        allData.append({text: figures})
        driver.back()

print(allData)
print(len(allData))
wb = Workbook()
ws1 = wb.active
for k in range(len(allData)):
    for key in allData[k].keys():
        ws1.cell(row=1, column=k + 1).value = key
    for value in allData[k].values():
        for j in range(len(value)):
            ws1.cell(row=j + 2, column=k + 1).value = value[j]

wb.save("hunan_figure.xlsx")
